#include "../weiconsole_ui_NativeUI.h"
#include <windows.h>
#include <stdlib.h>

#define CL_BLACK 0
#define CL_RED 1
#define CL_GREEN 2
#define CL_LIGHTRED 3
#define CL_BLUE 4
#define CL_PURPLE 5
#define CL_LIGHTBLUE 6
#define CL_GRAY 7
#define CL_NORMAL 8

JNIEXPORT void JNICALL Java_weiconsole_ui_NativeUI_setBackground
  (JNIEnv * env, jobject arg, jshort color)
{
	HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO csbi;

	if (!hCon) return;
	if (!GetConsoleScreenBufferInfo(hCon, &csbi)) return;

	switch (color) {
	case CL_BLACK:
		break;
	case CL_RED:
		SetConsoleTextAttribute(hCon, BACKGROUND_RED); 
		break;
	case CL_GREEN:
		SetConsoleTextAttribute(hCon, BACKGROUND_GREEN); 
		break;
	case CL_LIGHTRED:
		SetConsoleTextAttribute(hCon, BACKGROUND_RED | BACKGROUND_INTENSITY); 
		break;
	case CL_BLUE:
		SetConsoleTextAttribute(hCon, BACKGROUND_BLUE); 
		break;
	case CL_PURPLE:
		SetConsoleTextAttribute(hCon, BACKGROUND_RED | BACKGROUND_BLUE); 
		break;
	case CL_LIGHTBLUE:
		SetConsoleTextAttribute(hCon, BACKGROUND_BLUE | BACKGROUND_INTENSITY); 
		break;
	case CL_GRAY:
		break;
	case CL_NORMAL:
		SetConsoleTextAttribute(hCon, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN); 
		break;
	}
}

JNIEXPORT void JNICALL Java_weiconsole_ui_NativeUI_setForeground
  (JNIEnv * env, jobject arg, jshort color)
{
	HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	if (!hCon) return;
	if (!GetConsoleScreenBufferInfo(hCon, &csbi)) return;

	switch (color) {
	case CL_BLACK:
		break;
	case CL_RED:
		SetConsoleTextAttribute(hCon, FOREGROUND_RED); 
		break;
	case CL_GREEN:
		SetConsoleTextAttribute(hCon, FOREGROUND_GREEN); 
		break;
	case CL_LIGHTRED:
		SetConsoleTextAttribute(hCon, FOREGROUND_RED | FOREGROUND_INTENSITY); 
		break;
	case CL_BLUE:
		SetConsoleTextAttribute(hCon, FOREGROUND_BLUE); 
		break;
	case CL_PURPLE:
		SetConsoleTextAttribute(hCon, FOREGROUND_RED | FOREGROUND_BLUE); 
		break;
	case CL_LIGHTBLUE:
		SetConsoleTextAttribute(hCon, FOREGROUND_BLUE | FOREGROUND_INTENSITY); 
		break;
	case CL_GRAY:
		break;
	case CL_NORMAL:
		SetConsoleTextAttribute(hCon, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN); 
		break;
	}
}

JNIEXPORT void JNICALL Java_weiconsole_ui_NativeUI_cleanScreen
  (JNIEnv * env, jobject arg)
{
	system("cls");
}

static int savedX, savedY;

JNIEXPORT void JNICALL Java_weiconsole_ui_NativeUI_saveCursorPos
  (JNIEnv * env, jobject arg)
{
	HANDLE hCon;
	CONSOLE_SCREEN_BUFFER_INFO csbi;
    hCon = GetStdHandle(STD_OUTPUT_HANDLE);

	if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE ),&csbi)) 
		fprintf (stderr, "cannot get cursor position\n");
	else {
		savedX = csbi.dwCursorPosition.X;
		savedY = csbi.dwCursorPosition.Y;
	}
} 

JNIEXPORT void JNICALL Java_weiconsole_ui_NativeUI_restoreCursorPos
  (JNIEnv * env, jobject arg)
{
	HANDLE hCon;
	COORD setps;
    hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	
	setps.X = savedX;
	setps.Y = savedY;

	SetConsoleCursorPosition(hCon,setps);
}

JNIEXPORT void JNICALL Java_weiconsole_ui_NativeUI_moveCursor
  (JNIEnv * env, jobject arg, jint x, jint y)
{
	HANDLE hCon;
	COORD setps;
    hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	
	setps.X = x;
	setps.Y = y;

	SetConsoleCursorPosition(hCon,setps);
}

JNIEXPORT jint JNICALL Java_weiconsole_ui_NativeUI_getScrRow
  (JNIEnv * env, jobject arg)
{
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	
	if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE ),&csbi)) 
		fprintf (stderr, "cannot get terminal size\n");
	else
		return csbi.dwSize.Y;

	return 0;
}

JNIEXPORT jint JNICALL Java_weiconsole_ui_NativeUI_getScrCol
  (JNIEnv * env, jobject arg)
{
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	
	if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE ),&csbi)) 
		fprintf (stderr, "cannot get terminal size\n");
	else
		return csbi.dwSize.X;

	return 0;
}

